package prefuse.gui;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.*;
import  org.jdesktop.layout.GroupLayout;
/*
 * A class that load the existing xml file 
 * @author Wenyi An
 * May,2010
 */

public class UseExistingTreeGui extends javax.swing.JFrame {
		LoadVensimGui loadVensimUI;
		private InterfaceHandler interfaceHandler;
		
	    public UseExistingTreeGui(LoadVensimGui loadVensimUI,InterfaceHandler interfaceHandler) {
	    	this.interfaceHandler=interfaceHandler;
	    	this.loadVensimUI=loadVensimUI;
	    	this.vensimName=loadVensimUI.vensimLocation.getText().trim();
	        initComponents();
	    }

	    /** This method is called from within the constructor to
	     * initialize the form.
	     * WARNING: Do NOT modify this code. The content of this method is
	     * always regenerated by the Form Editor.
	     */
	    private void initComponents() {

	        specifyTree = new JLabel();
	        xmlLocaton = new JTextField();
	        browseFile = new JButton();
//	        startTime = new JLabel();
//	        starttimeInput = new JTextField();
	        runTreeBotton = new JButton();
	        backBotton = new JButton();
	        xmlErrorMsg = new JLabel();
//	        startTimeErrorMsg = new JLabel();
	        
	        setDefaultValue();	 
	        contentPaneLayout();
	        pack();
	        this.setLocationRelativeTo(null);
	    }// </editor-fold>

	

	    // Variables declaration - do not modify
	    private JButton browseFile;
	    private JButton runTreeBotton;
	    private JButton backBotton;
	    private JLabel specifyTree;
//	    private JLabel startTime;
	    private JTextField xmlLocaton;
//	    private JTextField starttimeInput;
	    private String vensimName;
	    private JLabel xmlErrorMsg;
//	    private JLabel startTimeErrorMsg;
	    
	    public void setDefaultValue(){
	    		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		        setTitle("Decision Tree");
		        specifyTree.setText("Please specify the Tree to Use (this should be an XML file previously produced by this software, but optionally incorporating some manual customizations)");
		        xmlLocaton.setText("C:\\tmp\\ann880testresult.xml");
		        browseFile.setText("Browse");
		        browseFile.addActionListener(new FileChooserListener());
//		        startTime.setText("Preferred Start Time");
//		        starttimeInput.setText("0.0");
		        runTreeBotton.setText("Create Visualization");
		        runTreeBotton.addActionListener(new BuildPictureListener());
		        backBotton.setText("Back");
		        backBotton.addActionListener(new BackListener()); 
		        xmlErrorMsg.setForeground(new Color(255, 0, 0));
//		        startTimeErrorMsg.setForeground(new Color(255, 0, 0));
	    }
	    
	    public void contentPaneLayout(){
	    	 org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
	    	 getContentPane().setLayout(layout);
		        layout.setHorizontalGroup(
		        		layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
		                .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
		                    .addContainerGap(324, Short.MAX_VALUE)
		                    .add(runTreeBotton)
		                    .add(153, 153, 153))
		                .add(layout.createSequentialGroup()
		                    .add(140, 140, 140)
		                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
		                        .add(specifyTree)
		                        .add(layout.createSequentialGroup()
		                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
//		                                .add(startTimeErrorMsg, /* org.jdesktop.layout.GroupLayout.LEADING, */ org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
		                                .add(org.jdesktop.layout.GroupLayout.LEADING, xmlLocaton)
//		                                .add(org.jdesktop.layout.GroupLayout.LEADING, startTime)
		                                .add(org.jdesktop.layout.GroupLayout.LEADING, backBotton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,  76,  org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
		                                .add(org.jdesktop.layout.GroupLayout.LEADING, xmlErrorMsg, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		                            .add(30, 30, 30)
		                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
		                                // .add(starttimeInput)
		                                .add(browseFile, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
		                    .addContainerGap(129, Short.MAX_VALUE))
		        );
		        layout.setVerticalGroup(
		        		 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
		                 .add(layout.createSequentialGroup()
		                     .add(69, 69, 69)
		                     .add(specifyTree)
		                     .add(33, 33, 33)
		                     .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
		                         .add(browseFile)
		                         .add(xmlLocaton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
		                     .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
		                     .add(xmlErrorMsg)
		                     .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
		                     //.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
		                     //    .add(startTime)
		                     //    .add(starttimeInput, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
		                     .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
//		                     .add(startTimeErrorMsg)
		                     .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 38, Short.MAX_VALUE)
		                     .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
		                         .add(runTreeBotton)
		                         .add(backBotton))
		                     .add(41, 41, 41))
			        );
	    }
	    // End of variables declaration
	    /**
		 * The listener for the press of the submit button.
		 * When it happens, obtain the data values from the fields, and
		 * go back to LoadVensim model interface.
		 */
		
		private class BackListener implements ActionListener
		{
			public void actionPerformed (ActionEvent event)
			{
				setVisible(false);
				loadVensimUI.setVisible(true);
	
			}
		}
	    /**
		 * The listener for the press of the submit button.
		 * When it happens, obtain the data values from the fields, and
		 * load the xml file,then run the tree.
		 */
		
		private class BuildPictureListener implements ActionListener
		{		
		
			public void actionPerformed (ActionEvent event)
			{			
				
//				xmlErrorMsg.removeAll();
//				xmlErrorMsg.setText("");
//				xmlErrorMsg.validate();
//				if(!xmlLocaton.getText().trim().isEmpty()){		
			
				if(xmlLocaton.getText().trim().length() > 0){					
						if(!xmlLocaton.getText().trim().endsWith(".xml")){
							xmlErrorMsg.setText("Not an xml file: " + xmlLocaton.getText());
							return;
						}	
						
						File file = new File(xmlLocaton.getText().trim());
						if(!file.exists()&&!file.isFile()){
							xmlErrorMsg.setText("Not an exist file: " + xmlLocaton.getText());
							return;
						}
						else{

							xmlErrorMsg.setText("it look very well");
							System.out.println("this is a test for load xmlfile       ");
							
							interfaceHandler.load(xmlLocaton.getText().trim());
							interfaceHandler.setXmlLocation(xmlLocaton.getText().trim());
							interfaceHandler.InitVensim(vensimName);
							try{
								
					//			interfaceHandler.run(/* starttimeInput.getText().trim() */);	
								
							}
							catch(Exception e){
//								startTimeErrorMsg.setText("Invalid time");
								System.err.println(e.getStackTrace());
								return;
							}					
							
						//	startTimeErrorMsg.setText("");						
							interfaceHandler.rollback();													
							setVisible(false);
							
							try {
								interfaceHandler.createPictureInterface();
							} catch (Exception e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}   // for now, no graphical interface; change to "TRUE" for a graphical interface
						}
					}
					else{
						xmlErrorMsg.setText("Empty field");
						return;
						}
				
			}
		}
		 /**
		 * The listener for choose file from the user's computer
		 * The user can choose the vensim file they want
		 */

		private class FileChooserListener implements ActionListener
		{	

			public void actionPerformed(ActionEvent e) {
				   JFileChooser c = new JFileChooser();
				   // Demonstrate "Open" dialog:
				   int rVal = c.showOpenDialog(UseExistingTreeGui.this);
				   if (rVal == JFileChooser.APPROVE_OPTION) {
					   xmlLocaton.setText(c.getCurrentDirectory().toString()+"\\"+c.getSelectedFile().getName());
				   }
				   if (rVal == JFileChooser.CANCEL_OPTION) {
					   xmlLocaton.setText("C:\\tmp\\KarenYeeDecisionTree_v3.xml");
				    
				   }
				 }
		}
		public static final long serialVersionUID = 1;
	}
